package tw.fc.agui;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.graphics.Typeface;

public final class GrCanvas extends Canvas {
   public final Paint DEFAULT_PAINT= new Paint(); 
   
   private final Path POLYGON_PATH= new Path();
   
   // ---------- construct ----------
   public GrCanvas() { 
      POLYGON_PATH.setFillType(Path.FillType.EVEN_ODD);   // 多邊形的填充方法預設為 EVEN_ODD
   }
   public GrCanvas(Bitmap bitmap) { 
      super(bitmap); 
      POLYGON_PATH.setFillType(Path.FillType.EVEN_ODD);
   }
   // ---------- construct ----------
   
   public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawArc(new RectF(x, y, x+width, y+height), startAngle, arcAngle, false, DEFAULT_PAINT);
   }
   public void drawArc(float x, float y, float width, float height, float startAngle, float arcAngle) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawArc(new RectF(x, y, x+width, y+height), startAngle, arcAngle, false, DEFAULT_PAINT);
   }
   public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawArc(new RectF(x, y, x+width, y+height), startAngle, arcAngle, false, DEFAULT_PAINT);
   }
   public void fillArc(float x, float y, float width, float height, float startAngle, float arcAngle) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawArc(new RectF(x, y, x+width, y+height), startAngle, arcAngle, false, DEFAULT_PAINT);
   }
   
   public void drawLine(int x1, int y1, int x2, int y2) {
      drawLine(x1, y1, x2, y2, DEFAULT_PAINT);
   }
   public void drawLine(float x1, float y1, float x2, float y2) {
      drawLine(x1, y1, x2, y2, DEFAULT_PAINT);
   }
   
   public void drawOval(int x, int y, int width, int height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawOval(new RectF(x, y, x+width, y+height), DEFAULT_PAINT);
   }
   public void drawOval(float x, float y, float width, float height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawOval(new RectF(x, y, x+width, y+height), DEFAULT_PAINT);
   }
   public void fillOval(int x, int y, int width, int height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawOval(new RectF(x, y, x+width, y+height), DEFAULT_PAINT);
   }
   public void fillOval(float x, float y, float width, float height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawOval(new RectF(x, y, x+width, y+height), DEFAULT_PAINT);
   }
   
   public void drawPath(Path p) {
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawPath(p, DEFAULT_PAINT);
   }
   public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.STROKE);
      POLYGON_PATH.moveTo(xPoints[0], yPoints[0]);
      for(int i=0; i<nPoints; i++) {
         POLYGON_PATH.lineTo(xPoints[i], yPoints[i]);
      }
      POLYGON_PATH.lineTo(xPoints[0], yPoints[0]);
      drawPath(POLYGON_PATH, DEFAULT_PAINT);
   }
   public void drawPolygon(float[] xPoints, float[] yPoints, int nPoints) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.STROKE);
      POLYGON_PATH.moveTo(xPoints[0], yPoints[0]);
      for(int i=0; i<nPoints; i++) {
         POLYGON_PATH.lineTo(xPoints[i], yPoints[i]);
      }
      POLYGON_PATH.lineTo(xPoints[0], yPoints[0]);
      drawPath(POLYGON_PATH, DEFAULT_PAINT);
   }
   public void drawPolygon(Path p) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawPath(p, DEFAULT_PAINT);
   } 
   public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.FILL);
      POLYGON_PATH.moveTo(xPoints[0], yPoints[0]);
      for(int i=0; i<nPoints; i++) {
         POLYGON_PATH.lineTo(xPoints[i], yPoints[i]);
      }
      POLYGON_PATH.lineTo(xPoints[0], yPoints[0]);
      drawPath(POLYGON_PATH, DEFAULT_PAINT);
   }
   public void fillPolygon(float[] xPoints, float[] yPoints, int nPoints) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.FILL);
      POLYGON_PATH.moveTo(xPoints[0], yPoints[0]);
      for(int i=0; i<nPoints; i++) {
         POLYGON_PATH.lineTo(xPoints[i], yPoints[i]);
      }
      POLYGON_PATH.lineTo(xPoints[0], yPoints[0]);
      drawPath(POLYGON_PATH, DEFAULT_PAINT);
   }
   public void fillPolygon(Path p) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawPath(p, DEFAULT_PAINT);
   }
   
   public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
      POLYGON_PATH.reset();
      DEFAULT_PAINT.setStyle(Style.STROKE);
      POLYGON_PATH.moveTo(xPoints[0], yPoints[0]);
      for(int i=0; i<nPoints; i++) {
         POLYGON_PATH.lineTo(xPoints[i], yPoints[i]);
      }
      drawPath(POLYGON_PATH, DEFAULT_PAINT);
   }
   
   public void drawRect(int x, int y, int width, int height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawRect(x, y, x+width, y+height, DEFAULT_PAINT);
   }
   public void drawRect(float x, float y, float width, float height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawRect(x, y, x+width, y+height, DEFAULT_PAINT);
   }
   public void fillRect(int x, int y, int width, int height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawRect(x, y, x+width, y+height, DEFAULT_PAINT);
   }
   public void fillRect(float x, float y, float width, float height) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawRect(x, y, x+width, y+height, DEFAULT_PAINT);
   }
   
   public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawRoundRect(new RectF(x, y, x+width, y+height), arcWidth, arcHeight,DEFAULT_PAINT);
   } 
   public void drawRoundRect(float x, float y, float width, float height, float arcWidth, float arcHeight) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.STROKE);
      drawRoundRect(new RectF(x, y, x+width, y+height), arcWidth, arcHeight,DEFAULT_PAINT);
   } 
   public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawRoundRect(new RectF(x, y, x+width, y+height), arcWidth, arcHeight,DEFAULT_PAINT);
   } 
   public void fillRoundRect(float x, float y, float width, float height, float arcWidth, float arcHeight) {
      if(width<0 || height<0) return;
      DEFAULT_PAINT.setStyle(Style.FILL);
      drawRoundRect(new RectF(x, y, x+width, y+height), arcWidth, arcHeight,DEFAULT_PAINT);
   } 
   
   public void drawString(String str, int x, int y) {
      drawText(str, x, y, DEFAULT_PAINT);
   }
   public void drawString(String str, float x, float y) {
      drawText(str, x, y, DEFAULT_PAINT);
   }
      
   
   public void setColor(int color) {
      DEFAULT_PAINT.setColor(color);
   }
   public void setColor(int a, int r, int g, int b) {
      DEFAULT_PAINT.setARGB(a, r, g, b);
   }
   public int getColor() {
      return DEFAULT_PAINT.getColor();
   }
   
   public void setAlpha(int alpha) {
      DEFAULT_PAINT.setAlpha(alpha);
   }
   public void setAntiAlias(boolean b) {
      DEFAULT_PAINT.setAntiAlias(b);
   }
   
   public void setPolygonFillPolicy(Path.FillType fillType) {
      POLYGON_PATH.setFillType(fillType);
   }
   
   public void setTextSize(float textSize) {
      DEFAULT_PAINT.setTextSize(textSize);
   }   
   public float getTextSize() {
      return DEFAULT_PAINT.getTextSize();
   }
   public void setTypeface(Typeface typeface) {
      DEFAULT_PAINT.setTypeface(typeface);
   }
}
